<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    pieData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: this.pieData.name,
          x: 'center',
          textStyle: {
            lineHeight: 22,
            fontWeight: 'normal',
            color: '#fff'
          },
          padding: 16
        },
        /* legend: {
            left: 'center',
            bottom: '10',
            data: this.name
          },*/
        color: this.pieData.theme,
        series: [
          {
            name: this.name,
            type: 'pie',
            labelLine: {
              show: false
            },
            legendHoverLink: false,
            hoverAnimation: false,
            emphasis: {
              label: {
                show: true
              }
            },
            label: {
              show: true,
              position: 'center',
              formatter: function(params) {
                if (params.name !== '' && params.name !== null) {
                  return params.data.value
                }
                return ''
              },
              textStyle: {
                fontSize: 32,
                color: '#39cccc'
              }
            },
            center: ['50%', '59%'],
            radius: ['50%', '78%'],
            data: this.pieData.data,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>

<style>

</style>
